package com.cakin.javademo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: GroupCount
 * @Description: 分组计数 参考：http://www.cielni.com/2018/07/14/java-stream-groupingby/
 * @Date: 2021/3/13
 * @Author: cakin
 */
public class GroupCount {
    /**
     * 功能描述：分组计数
     *
     * @author cakin
     * @date 2021/3/14
     * @param args 命令行
     * @description: 以 产品类型 和 国家 多字段为关键字，进行计数
     */
    public static void main(String[] args) {
        List<Record> records = new ArrayList<>();
        records.add(new Record("电脑", "中国", "台式"));
        records.add(new Record("洗衣机", "韩国", "滚筒"));
        records.add(new Record("电脑", "中国", "笔记本"));
        records.add(new Record("洗衣机", "韩国", "一般"));
        records.add(new Record("电脑", "中国", "平板"));
        records.add(new Record("洗衣机", "中国", "笔记本"));
        records.add(new Record("电脑", "韩国", "滚筒"));
        records.add(new Record("电脑", "韩国", "平板"));
        records.add(new Record("洗衣机", "中国", "一般"));


        // 以 productType 和 country 两个字段为关键字，进行计数（Collectors.counting()）
        Map<String, Long> countMap = records
                .stream()
                .collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));

        List<Record> countRecords = countMap.keySet().stream().map(key -> {
            String[] temp = key.split("_");
            String productType = temp[0];
            String country = temp[1];
            Record record = new Record();
            record.setProductType(productType);
            record.setCountry(country);
            record.setCount(countMap.get(key).intValue());
            return record;
        }).collect(Collectors.toList());
        for (Record countRecord : countRecords) {
            System.out.println(countRecord);
        }
    }
}

class Record {
    /**
     * 产品类型
     */
    private String productType;
    /**
     * 国家
     */
    private String Country;
    /**
     * 风格
     */
    private String style;
    /**
     * 计数
     */
    private int count;

    public String getProductType() {
        return productType;
    }

    public void setProductType(String productType) {
        this.productType = productType;
    }

    public String getCountry() {
        return Country;
    }

    public void setCountry(String country) {
        Country = country;
    }

    public Record() {
    }

    public Record(String productType, String country, String style) {

        this.style = style;
        this.productType = productType;
        this.Country = country;
    }

    public String getStyle() {
        return style;
    }

    public void setStyle(String style) {
        this.style = style;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public String toString() {
        return "Record{" +
                "productType='" + productType + '\'' +
                ", Country='" + Country + '\'' +
                ", count=" + count +
                '}';
    }
}


